<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="">
  <meta name="format-detection" content="telephone=no">
  <meta name="full-screen" content="yes">
  <meta name="x5-page-mode" content="app">
  <meta name="x5-fullscreen" content="true">
  <meta name="browsermode" content="application">
  <meta content="" name="keywords">
  <meta content="" name="description">
  <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
  <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
  <link rel="stylesheet" href="./js/layer_mobile/need/layer.css">
  <link rel="stylesheet" href="./css/style.css?ver=1.1">
  <title>商户申请</title>
  <style>
    .weui-btn_primary[disabled] { opacity: .7; }
    #pay-speed { display: none; }
  </style>
</head>
<body>
  <div class="page__hd">
    <div class="page__title">商户申请</div>
    <div class="page__desc">请 <span class="theme-color">如实</span> 填写您的信息</div>
  </div>
  <!-- 基本信息 -->
  <div class="weui-cells weui-cells_form">
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <input id='company' class="weui-input" type="text" placeholder="请输入你的公司名">
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <input id='name' class="weui-input" type="text" placeholder="请输入姓名">
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <input id='tel' class="weui-input" type="tel" placeholder="请输入手机号">
      </div>
    </div>
  </div>

  <!-- 服务条款 -->
  <!-- <label for="weuiAgree" class="weui-agree">
    <input id="weuiAgree" type="checkbox" class="weui-agree__checkbox">
    <span class="weui-agree__text">
      阅读并同意<a href="javascript:void(0);">《相关条款》</a>
    </span>
  </label> -->
  <button class="weui-btn weui-btn_primary submit-btn" id="pay-btn">申请成为商户</button>
  <a class="weui-btn weui-btn_primary submit-btn" href="./applySpeed.html" id="pay-speed">查看进度</a>


  <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
  <script src="./js/tools2.js?ver=2.1"></script>
  <script src="./js/jweixin-1.4.0.js?ver=1.0"></script>
  <script src="./js/layer_mobile/layer.js"></script>
  <script>
    // var vConsole = new VConsole();
    var pid = tools.getQueryString('proxy_id');   // 代理的id
    var mid = tools.getItem('userInfo').user_code;   // 用户id

    var conf = {
      flag: false,   // 用于验证是否有申请成为商户
      isProxy: false,   // 是否是代理
      pmid: 0,          // 如果是代理，这个代理的用户id
    }
    $(function() {
      tools.login(function() {
        getSpeed();
        getIsProxy();
      })

      // 申请
      $('#pay-btn').on('click', function() {
        if(!validate()) return;
        layer.open({ type: 2, shadeClose: false });
        tools.ajax({
          type: 'POST',
          url: '/service/mch',
          data: {
            'proxy_id': pid,
            name: $('#name').val(),
            company: $('#company').val(),
            tel: $('#tel').val()
          },
          success: function(res) {
            if(res.code == 0) {
              window.location.href = 'applyMsg.html'
            } else {
              layer.open({ content: res.msg, skin: 'msg', time: 2 });
            }
          },
          complete: function(xhr) {
            layer.closeAll();
          }
        })
      })
    })

    // 验证
    function validate() {
      var telReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
      if(!pid) {
        // 没有通过代理
        layer.open({ content: '请通过代理在申请商户', skin: 'msg', time: 2 });
        return false;
      } /* else if(conf.isProxy) {
        layer.open({ content: '代理不能成为商户', skin: 'msg', time: 2 });
        return false;
      } */ else if(mid == conf.pmid) {
        layer.open({ content: '不能成为自己的商户', skin: 'msg', time: 2 });
        return false;
      } else if(conf.flag) {
        // 已经申请
        layer.open({ content: '您已申请成为商户', skin: 'msg', time: 2 });
        return false;
      } else if(!$('#company').val()) {
        layer.open({ content: '请填写公司名', skin: 'msg', time: 2 });
        return false
      } else if(!$('#name').val()) {
        layer.open({ content: '请填写姓名', skin: 'msg', time: 2 });
        return false;
      } else if(!$('#tel').val()) {
        layer.open({ content: '请填写手机号', skin: 'msg', time: 2 });
        return false;
      } else if(!telReg.test($('#tel').val())) {
        layer.open({ content: '手机号格式不正确', skin: 'msg', time: 2 });
        return false;
      } /* else if(!$('#weuiAgree').is(":checked")) {
        layer.open({ content: '请阅读并同意相关条款', skin: 'msg', time: 2 });
        return false;
      } */
      return true;
    }

    // 申请进度
    function getSpeed() {
      tools.ajax({
        url: '/service/mch_info',
        success: function(res) {
          if(res.code == 0) {
            conf.flag = true
            $('#company').attr({"disabled":true}).val(res.data.company);
            $('#name').attr({"disabled":true}).val(res.data.name);
            $('#tel').attr({"disabled":true}).val(res.data.tel);
            $('#pay-btn').css({"display": "none"});
            $('#pay-speed').css({"display": "block"});
          }
        }
      })
    }

    // 当前用户是否是代理，是代理的话，不能通过自己成为商户
    function getIsProxy() {
      tools.ajax({
        url: "/proxy/" + mid,
        success: function(res) {
          if(res.code == 0) {
            conf.isProxy = true;
            conf.pmid = res.data.mid;
          }
        }
      })
    }

    wx.ready(function() {
      var options = { title: "商户申请", desc: "申请成为天空文化传播的商户" };
      tools.shareTimeline(options)
      tools.shareAppMessage(options)
    })
  </script>
</body>
</html>